package mi.payment.admin.pay88;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import lainey.ui.dialog.EditTextDialog;
import lainey.ui.widget.TitleView;
import mi.payment.R;
import mi.payment.ui.base.ParentActivity;

public class IPay88MerchantActivity extends ParentActivity {

    private TextView mTvMerchantName;
    private TextView mTvAdd1;
    private TextView mTvAdd2;
    private TextView mTvAdd3;
    private TextView mTvMerchantCode;
    private TextView mTvMerchantID;
    private TextView mTvMerchantKey;
    private TextView mTvTransTimeout;

    private static final int NAME = 1;
    private static final int ADD1 = 2;
    private static final int ADD2 = 3;
    private static final int ADD3 = 4;
    private static final int CODE = 5;
    private static final int ID = 6;
    private static final int KEY = 7;
    private static final int TIMEOUT = 8;
    private EditTextDialog mEditTextDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.admin_pay88_merchant_activity);
        initView();
    }

    private void initView() {
        IPay88Bean bean = IPay88Operation.getInstance().getIPay88Bean();

        TitleView titleView = findViewById(R.id.title_view);
        titleView.setCenterText(R.string.pay88_setting_merchant);
        titleView.setLeftImageOnClickListener(
                v -> onBackPressed()
        );

        View item = findViewById(R.id.item_merchant_name);
        item.findViewById(R.id.left_image).setVisibility(View.GONE);
        TextView leftText = item.findViewById(R.id.left_text);
        mTvMerchantName = item.findViewById(R.id.right_text);
        item.setOnClickListener(this);
        leftText.setText(R.string.pay88_setting_merchant_name);
        if (bean.merchantName != null && bean.merchantName.length() > 0) {
            mTvMerchantName.setText(bean.merchantName);
        } else {
            mTvMerchantName.setText("--");
        }

        item = findViewById(R.id.item_add1);
        item.findViewById(R.id.left_image).setVisibility(View.GONE);
        leftText = item.findViewById(R.id.left_text);
        mTvAdd1 = item.findViewById(R.id.right_text);
        item.setOnClickListener(this);
        leftText.setText(R.string.pay88_setting_merchant_add1);
        if (bean.addLine1 != null && bean.addLine1.length() > 0) {
            mTvAdd1.setText(bean.addLine1);
        } else {
            mTvAdd1.setText("--");
        }

        item = findViewById(R.id.item_add2);
        item.findViewById(R.id.left_image).setVisibility(View.GONE);
        leftText = item.findViewById(R.id.left_text);
        mTvAdd2 = item.findViewById(R.id.right_text);
        item.setOnClickListener(this);
        leftText.setText(R.string.pay88_setting_merchant_add2);
        if (bean.addLine2 != null && bean.addLine2.length() > 0) {
            mTvAdd2.setText(bean.addLine2);
        } else {
            mTvAdd2.setText("--");
        }

        item = findViewById(R.id.item_add3);
        item.findViewById(R.id.left_image).setVisibility(View.GONE);
        leftText = item.findViewById(R.id.left_text);
        mTvAdd3 = item.findViewById(R.id.right_text);
        item.setOnClickListener(this);
        leftText.setText(R.string.pay88_setting_merchant_add3);
        if (bean.addLine3 != null && bean.addLine3.length() > 0) {
            mTvAdd3.setText(bean.addLine3);
        } else {
            mTvAdd3.setText("--");
        }

        item = findViewById(R.id.item_merchant_code);
        item.findViewById(R.id.left_image).setVisibility(View.GONE);
        leftText = item.findViewById(R.id.left_text);
        mTvMerchantCode = item.findViewById(R.id.right_text);
        item.setOnClickListener(this);
        leftText.setText(R.string.pay88_setting_merchant_code);
        if (bean.merchantCode != null && bean.merchantCode.length() > 0) {
            mTvMerchantCode.setText(bean.merchantCode);
        } else {
            mTvMerchantCode.setText("--");
        }

        item = findViewById(R.id.item_merchant_id);
        item.findViewById(R.id.left_image).setVisibility(View.GONE);
        leftText = item.findViewById(R.id.left_text);
        mTvMerchantID = item.findViewById(R.id.right_text);
        item.setOnClickListener(this);
        leftText.setText(R.string.pay88_setting_merchant_id);
        if (bean.merchantID != null && bean.merchantID.length() > 0) {
            mTvMerchantID.setText(bean.merchantID);
        } else {
            mTvMerchantID.setText("--");
        }

        item = findViewById(R.id.item_merchant_key);
        item.findViewById(R.id.left_image).setVisibility(View.GONE);
        leftText = item.findViewById(R.id.left_text);
        mTvMerchantKey = item.findViewById(R.id.right_text);
        item.setOnClickListener(this);
        leftText.setText(R.string.pay88_setting_merchant_key);
        if (bean.merchantKey != null && bean.merchantKey.length() > 0) {
            mTvMerchantKey.setText(bean.merchantKey);
        } else {
            mTvMerchantKey.setText("--");
        }

        item = findViewById(R.id.item_timeout);
        item.findViewById(R.id.left_image).setVisibility(View.GONE);
        leftText = item.findViewById(R.id.left_text);
        mTvTransTimeout = item.findViewById(R.id.right_text);
        item.setOnClickListener(this);
        leftText.setText(R.string.pay88_setting_merchant_timeout);
        mTvTransTimeout.setText(bean.transTimeout + "");
    }

    @Override
    public void onClick(View v) {
        final int id = v.getId();
        switch (id) {
            case R.id.item_merchant_name:
                showEditTextDialog(NAME, R.string.pay88_setting_merchant_name_hint, IPay88Operation.getInstance().getIPay88Bean().merchantName);
                break;
            case R.id.item_add1:
                showEditTextDialog(ADD1, R.string.pay88_setting_merchant_add1_hint, IPay88Operation.getInstance().getIPay88Bean().addLine1);
                break;
            case R.id.item_add2:
                showEditTextDialog(ADD2, R.string.pay88_setting_merchant_add2_hint, IPay88Operation.getInstance().getIPay88Bean().addLine2);
                break;
            case R.id.item_add3:
                showEditTextDialog(ADD3, R.string.pay88_setting_merchant_add3_hint, IPay88Operation.getInstance().getIPay88Bean().addLine3);
                break;
            case R.id.item_merchant_code:
                showEditTextDialog(CODE, R.string.pay88_setting_merchant_code_hint, IPay88Operation.getInstance().getIPay88Bean().merchantCode);
                break;
            case R.id.item_merchant_id:
                showEditTextDialog(ID, R.string.pay88_setting_merchant_id_hint, IPay88Operation.getInstance().getIPay88Bean().merchantID);
                break;
            case R.id.item_merchant_key:
                showEditTextDialog(KEY, R.string.pay88_setting_merchant_key_hint, IPay88Operation.getInstance().getIPay88Bean().merchantKey);
                break;
            case R.id.item_timeout:
                showEditTextDialog(TIMEOUT, R.string.pay88_setting_merchant_timeout_hint, "" + IPay88Operation.getInstance().getIPay88Bean().transTimeout);
                break;
        }
    }

    private void showEditTextDialog(int type, int title, String hint) {
        mEditTextDialog = new EditTextDialog(this);
        mEditTextDialog.setRightText(R.string.ok);
        mEditTextDialog.setLeftText(R.string.cancel);
        mEditTextDialog.setCanceledOnTouchOutside(true);
        mEditTextDialog.setCancelable(true);
        mEditTextDialog.setTextMode();
        mEditTextDialog.setLeftOnClickListener(
                v -> dismissEditTextDialog()
        );
        mEditTextDialog.setRightOnClickListener(
                v -> {
                    String text = mEditTextDialog.getText();
                    IPay88Bean bean = IPay88Operation.getInstance().getIPay88Bean();
                    if (text.trim().length() > 0) {
                        if (type == NAME) {
                            bean.merchantName = text;
                            mTvMerchantName.setText(text);
                        } else if (type == ADD1) {
                            bean.addLine1 = text;
                            mTvAdd1.setText(text);
                        } else if (type == ADD2) {
                            bean.addLine2 = text;
                            mTvAdd2.setText(text);
                        } else if (type == ADD3) {
                            bean.addLine3 = text;
                            mTvAdd3.setText(text);
                        } else if (type == CODE) {
                            bean.merchantCode = text;
                            mTvMerchantCode.setText(text);
                        } else if (type == ID) {
                            bean.merchantID = text;
                            mTvMerchantID.setText(text);
                        } else if (type == KEY) {
                            bean.merchantKey = text;
                            mTvMerchantKey.setText(text);
                        } else if (type == TIMEOUT) {
                            try {
                                int parseInt = Integer.parseInt(text);
                                if (parseInt >= 30 && parseInt <= 60) {
                                    bean.transTimeout = parseInt;
                                    mTvTransTimeout.setText(text);
                                } else {
                                    showToast(R.string.error_input);
                                    return;
                                }
                            } catch (Exception e) {
                                showToast(R.string.error_input);
                                return;
                            }
                        } else {
                            showToast(R.string.error_input);
                            return;
                        }

                        dismissEditTextDialog();
                        IPay88Operation.getInstance().saveIPay88Bean(bean);
                    } else {
                        showToast(R.string.error_input);
                    }
                }
        );

        mEditTextDialog.setTitleText(title);
        if (type == TIMEOUT) {
            mEditTextDialog.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
            mEditTextDialog.setMaxLength(20);
        } else {
            mEditTextDialog.setInputType(EditorInfo.TYPE_CLASS_TEXT);
            mEditTextDialog.setMaxLength(60);
        }
        boolean equals = TextUtils.isEmpty(hint)
                || TextUtils.equals(getString(R.string.unfilled), hint)
                || TextUtils.equals("--", hint);
        if (equals) {
            mEditTextDialog.setText("");
        } else {
            mEditTextDialog.setText(hint);
        }
        try {
            mEditTextDialog.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void dismissEditTextDialog() {
        if (mEditTextDialog != null) {
            try {
                mEditTextDialog.dismiss();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


}
